package com.bys.systemclock;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bigkoo.pickerview.TimePickerView;
import android.widget.Toast;

import com.bys.managercolok.AlarmManagerUtil;
import com.bys.managercolok.SimpleDialog;
import com.bys.systemclock.view.SelectRemindCyclePopup;
import com.bys.systemclock.view.SelectRemindWayPopup;

import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Formatter;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout allLayout;
    private Button set_btn;
    private TextView date_tv,tv_repeat_value,tv_ring_value;
    private RelativeLayout repeat_rl,ring_rl;
    private  TimePickerView pvTime;
//    选择的时间
    private String time;
//    选择提醒的周期
    private  int cycle;
//    选择提醒的铃声方式
    private int ringWay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//         容器布局
        allLayout = findViewById(R.id.all_layout);
//      确定
        set_btn = findViewById(R.id.set_btn);
        set_btn.setOnClickListener(this);
//        选择时间文本
        date_tv = findViewById(R.id.date_tv);
//      重复界面
        repeat_rl = findViewById(R.id.repeat_r1);
        repeat_rl.setOnClickListener(this);
//         铃声 界面
        ring_rl = findViewById(R.id.ring_rl);
        ring_rl.setOnClickListener(this);
        tv_repeat_value = findViewById(R.id.tv_repeat_value);
        tv_ring_value = findViewById(R.id.tv_ring_value);
        pvTime = new TimePickerView(this, TimePickerView.Type.HOURS_MINS);
        pvTime.setTime(new Date());
//        是否周期的
        pvTime.setCyclic(false);
        pvTime.setCancelable(true);
//      时间选择后回调
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                time = getTime(date);
                Log.e("bys", "onTimeSelect: "+time);
                date_tv.setText(time);
            }
        });
        date_tv.setOnClickListener((view) ->{
            pvTime.show();
        } );


    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(date);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.repeat_r1:
                selectRemindCycle();
                break;
            case R.id.ring_rl:
                selectRingWay();
                break;
            case R.id.set_btn:
                goOffClock();
                break;
            default:
                break;
        }
    }

    /**
     * 设置 时间
     */
    private void goOffClock() {
//        Toast.makeText(MainActivity.this,"点击了确定按钮",Toast.LENGTH_SHORT).show();
        String[] times = time.split(":");
        if("".equals(time) || null== time){
            Toast.makeText(this,"请选择时间",Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(this,"闹钟设置成功",Toast.LENGTH_SHORT).show();
            if(cycle == 0){
//                是每天的闹钟
                AlarmManagerUtil.setAlarm(this,0,Integer.parseInt(times[0]),Integer.parseInt(times[1]),0,0,"快起床，快起床啦",ringWay);
            }else if (cycle == -1){
//                是只响一次的闹钟
                AlarmManagerUtil.setAlarm(this,1, Integer.parseInt(times[0]), Integer.parseInt(times[1]),0,0,"我是只响一次的闹钟",ringWay);

            }else{
//                多选的闹钟
            }
        }
    }

    /**
     * 选择 铃声 途径
     */
    private void selectRingWay() {
//        Toast.makeText(MainActivity.this,"点击了铃声界面",Toast.LENGTH_SHORT).show();
        SelectRemindWayPopup selectRemindWayPopup = new SelectRemindWayPopup(MainActivity.this);
        selectRemindWayPopup.showPopup(allLayout);
        selectRemindWayPopup.setSelectRemindWayPopupListener((flag -> {
            switch (flag){
                case 0:
                    tv_ring_value.setText("震动");
                    Log.e("bys", "selectRingWay:  震动");
                    ringWay = 0;
                    break;
                case 1:
                    tv_ring_value.setText("铃声");
                    ringWay = 1;
                    break;
            }
        }));

    }

    /**
     *  提醒时机
     */
    private void selectRemindCycle() {
//        Toast.makeText(MainActivity.this,"点击了重复界面",Toast.LENGTH_SHORT).show();
        SelectRemindCyclePopup remindCyclePopup = new SelectRemindCyclePopup(MainActivity.this);
        remindCyclePopup.showPopup(allLayout);
        remindCyclePopup.setOnSelectRemindCyclePopupListener(new SelectRemindCyclePopup.SelectReindCyclePopupListener() {
            @Override
            public void obtainMessage(int flag, String ret) {
                switch (flag){
                    case 0:
//                        星期一
                        break;
                    case 1:
//                        星期二
                        break;
                    case 2:
//                        星期三
                        break;
                    case 3:
//                        星期四
                        break;
                    case 4:
//                        星期五
                        break;
                    case 5:
//                        星期六
                        break;
                    case 6:
//                        星期日
                        break;
                    case 7:
//                        选择的周一
                        int repeat = Integer.parseInt(ret);
                        tv_repeat_value.setText(parseRepeat(repeat,0));
                         cycle = repeat;
                        remindCyclePopup.dismiss();
//                        确定
                        break;
                    case 8:
                        tv_repeat_value.setText("每天");
                        cycle = 0;
                        remindCyclePopup.dismiss();
//                        每天
                        break;
                    case 9:
//                        只响一次
                        tv_repeat_value.setText("只响一次");
                        cycle = -1;
                        remindCyclePopup.dismiss();
                        break;
                    default:
                        break;
                }
            }
        });


    }

    /**
     * 解析选择的提醒重复周期
     * @param repeat
     * @param flag
     * @return
     */
    private String parseRepeat(int repeat, int flag) {
        String cycle = "";
        String weeks = "";
//            周一到周日 总数 是 127 2的 8 次方
        if(repeat == 0)
            repeat = 127;

        if(repeat % 2 == 1){
            cycle = "周一";
            weeks = "1";
        }
        if(repeat % 4 >= 2){
            if("".equals(cycle)){
                cycle ="周二";
                weeks = "2";
            }else {
                cycle = cycle + "," + "周二";
                weeks = weeks  + "," + "2";
            }
        }
        if(repeat % 8 >= 4){
            if("".equals(cycle)){
                cycle ="周三";
                weeks = "3";
            }else {
                cycle = cycle + "," + "周三";
                weeks = weeks  + "," + "3";
            }
        }
        if(repeat % 16 >= 8){
            if("".equals(cycle)){
                cycle ="周四";
                weeks = "4";
            }else {
                cycle = cycle + "," + "周四";
                weeks = weeks  + "," + "4";
            }
        }
        if(repeat % 32 >= 16){
            if("".equals(cycle)){
                cycle ="周五";
                weeks = "5";
            }else {
                cycle = cycle + "," + "周五";
                weeks = weeks  + "," + "5";
            }
        }
        if(repeat % 64 >= 32){
            if("".equals(cycle)){
                cycle ="周六";
                weeks = "6";
            }else {
                cycle = cycle + "," + "周六";
                weeks = weeks  + "," + "6";
            }
        }
        if(repeat / 64 >= 1){
            if("".equals(cycle)){
                cycle ="周日";
                weeks = "7";
            }else {
                cycle = cycle + "," + "周日";
                weeks = weeks  + "," + "7";
            }
        }
        return flag == 0 ? cycle : weeks;
    }

}